<?php defined('SYSPATH') or die('No direct script access.');

class Controller_AJAX_Task extends Controller {
    
	
	public function action_index()
	{
		$ret = Session::instance()->set('lover', 'To Anh');
		echo Debug::dump($ret);
		//echo ('In the session controler');
	}
	public function action_add_task($project_id)
	{
		/*
		$t = Model::factory('task');
		$info['name'] 			= $_POST['name'];
		$info['project_id']		= $_POST['project_id'];
		$info['priority']		= $_POST['priority'];
		$info['deadline']		= $_POST['deadline'];
		$info['description']	= $_POST['description'];
		$info['attachment']		= '';
		$info['status']			= Helper_MyConst::STATUS_IN_PROGRESS;
		
		$assign_user 	= $_POST['assign_user'];
		$ret = $t->add_task(Helper_Auth::instance()->get_user_id(), $info);
		$ret = $t->assign_task($ret[0], $assign_user);
		*/
		$m	= Model::factory('task');
		$user_id = Helper_Auth::instance()->get_user_id();
		$data = $_POST;
		if ($data['user_id'] != $user_id)
			return false;

		//add task
		$ret = $m->add_task($data);
		if ($ret)
		{
			$data['id'] = $ret[0];
			$ret_array = array(
				'success' => TRUE,
				'data' => $data
			);
		}
		else 
			$ret_array = array(
				'success' => FALSE
			);
		$this->response->body(json_encode($ret_array));
	}
	public function action_get_add_task_group($project_id)
	{
		$lists = Model::factory('task')->get_project_group($project_id);
		$this->response->body(View::factory('/task/partials/group_list',array(
			'lists' => $lists
		)));
	}
	public function action_close_task()
	{
		$this->response->body(json_encode(
			array(
				'success' => TRUE,
				'id'	  => 1//$ret[0]
			)
		));
	}
	public function action_open_task()
	{
		$this->response->body(json_encode(
			array(
				'success' => TRUE,
				'id'	  => 1//$ret[0]
			)
		));
	}
	public function action_delete_task()
	{
		$this->response->body(json_encode(
			array(
				'success' => TRUE,
				'id'	  => 1//$ret[0]
			)
		));
	}
	public function action_edit_task()
	{
		$data = $_POST;
		$ret = Model::factory('task')->edit_task($data);
		if ($ret)
			$ret_array = array(
				'success' => TRUE,
				'data'	  => $data,
				'id'	  => $ret[0]
			);
		else 
			$ret_array = array(
				'success' => FALSE
			);
		$this->response->body(json_encode(
			$ret_array
		));
	}
	public function action_add_group($project_id)
	{
		$name 		= $_GET['name'];
		$user_id 	= $_GET['user_id'];
		$user = Helper_Auth::instance()->get_user_id();
		if ($user['id'] != $user_id)
			return false;
			
		$ret = Model::factory('task')->add_group($name, $project_id, $user_id);
		if ($ret)
			$res = array(
				'success' => true,
				'data' => array(
					'id'	  => $ret[0],			
					'name'	  => $name
			));
		else
			$res = array(
				'success' => false,
			);
		$this->response->body(json_encode($res));
	}
}